/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite.
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.definition;


import java.util.Collection;
import java.util.List;

import dex.compiler.model.base.ParseNode;
import dex.misc.Conversion;


/**
 * A class definition.
 */
public class ClassDef extends UserType {
	

	/**
	 * The superclass of the class being defined.
	 */
	private String superclass;


	/**
	 * Constructs a new class definition.
	 * 
	 * @param params  defines the place, name, attributes, modifier and comment for this class
	 * @param superclass  the superclass of the class being defined
	 * @param contents    the members of the class being defined
	 */
	public ClassDef(DefinitionParams params, String superclass, List<ParseNode> contents) {
		super(params, contents);
		this.superclass = superclass;
	}


	/**
	 * Returns the fields defined by this class.
	 * 
	 * @return  the fields
	 */
	public Collection<TypedName> getFields() {
		return Conversion.subclasses(getContents(), TypedName.class);
	}


	/**
	 * Returns the methods defined by this class.
	 * 
	 * @return  the methods
	 */
	public Collection<Function> getMethods() {
		return Conversion.subclasses(getContents(), Function.class);
	}


	/**
	 * Returns the name of the superclass of this class.
	 * 
	 * @return  the superclass.
	 */
	public String getSuperclass() {
		return superclass;
	}


	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		print(result);
		result.append("class ").append(getName()).append(" extends ").append(superclass);
		result.append(" {\n");
		for (ParseNode node : getContents()) {
			result.append("\n\n");
			result.append(node);
		}
		result.append("}\n\n");
		return result.toString();
	}


}
